package cn.itcast.service.impl;

import cn.itcast.constant.MessageConstant;
import cn.itcast.dao.OrderDao;
import cn.itcast.dao.OrderSettingDao;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderService;
import cn.itcast.util.IdWorker;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private IdWorker idWorker ;

    /**分页查询**/
    @Override
    public PageResult findPage(OrderQueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //查询规则
        PageHelper.startPage(currentPage,pageSize);
        Page<Order> page = orderDao.findPage(queryPageBean);
        //查询的总条数
        long total = page.getTotal();
        //查询的当前页的集合
        List<Order> result = page.getResult();
        return new PageResult(total,result);
    }

    /**新增预约信息**/
    @Override
    public Result submit(Order order) {
        if (order.getId() == null) {
            //没有预约数据 , 可以进行预约
            order.setOrderStatus(0);
            order.setArrival(0);
            order.setId(idWorker.nextId() + "");
            order.setPayStatus(0);

            //保存预约数据
            orderDao.submit(order);

        }
        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());

        /*//1. 判断是否设置过预约 , 如果当天没有设置过预约则无法预约
        Date orderDate = order.getOrderDate();
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2. 判断是否约满 , 如果当天约满则无法预约
        if (orderSetting.getNumber() <= orderSetting.getReservations()) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //3. 判断是否重复预约  , 如果同一天已经预约过 , 则无法预约
        Order condition = new Order();
        condition.setIdCard(order.getIdCard());
        condition.setSetmealId(order.getSetmealId());
        condition.setOrderDate(orderDate);
        //查询预约数据
        Long count = orderDao.findOrderCountByCondition(condition);
        //有预约数据, 重复预约
        if (count > 0) {
            return new Result(false, MessageConstant.HAS_ORDERED);
        }

        if (order.getId() == null) {
            //没有预约数据 , 可以进行预约
            order.setOrderStatus(0);
            order.setArrival(0);
            order.setId(idWorker.nextId()+"");
            order.setPayStatus(0);

            //保存预约数据
            orderDao.submit(order);

            //5. 预约成功更新预约人数
            orderSetting.setReservations(orderSetting.getReservations() + 1);
            orderSettingDao.editReservationsByOrderDate(orderSetting);
            return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
        }

        //更新预约信息
        orderDao.update(order);

        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());*/
    }

    /**更新预约信息**/
    @Override
    public Result update(Order order) {
        orderDao.update(order);
        return new Result(true, MessageConstant.ORDER_SUCCESS);
    }

    /**根据id查询预约信息**/
    @Override
    public Order findById(String id) {

        return orderDao.findById(id);
    }

    /**根据id删除预约信息**/
    @Override
    public void deleteById(String id) {

        orderDao.deleteById(id);
    }

    /**根据id修改用户预约状态**/
    @Override
    public void arrivalEdit(int event, String id) {
        orderDao.arrivalEdit(event,id);
    }
}
